package com.funcablaze.window;

import com.funcablaze.Application;
import com.funcablaze.component.Scene.Scene;
import com.funcablaze.component.control.Theme;
import com.funcablaze.listener.Input;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;

import static org.lwjgl.glfw.GLFW.GLFW_ALPHA_BITS;
import static org.lwjgl.glfw.GLFW.glfwWindowHint;
import static org.lwjgl.opengl.GL11.*;

public class Frame extends BaseWindow {
	
	public Frame(String title, int width, int height) {
		_create(title, width, height);
	}
	
	/**
	 * 创建窗口
	 * @param title 窗口标题
	 * @param width 窗口宽度
	 * @param height 窗口高度
	 */
	protected void _create(String title, int width, int height) {
		this.width = width;
		this.height = height;
		glfwWindowHint(GLFW_ALPHA_BITS, 8); // 确保有 Alpha 通道
		windowHandle = GLFW.glfwCreateWindow(width, height, title, 0, 0);
		// 设置OpenGL上下文
		GLFW.glfwMakeContextCurrent(windowHandle);
		GL.createCapabilities(); // 关键！
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		// 设置窗口大小改变回调
		GLFW.glfwSetFramebufferSizeCallback(windowHandle, (win, w, h) -> {
			this.width = w;
			this.height = h;
			GL11.glViewport(0, 0, w, h);
			updateProjection(w, h);
		});
		// 设置窗口关闭回调
		GLFW.glfwSetWindowCloseCallback(windowHandle, windowHandle -> {
			if (windowListener.onClose != null) windowListener.onClose.onClose();
			else close();
		});
		// 设置窗口最大化回调
		GLFW.glfwSetWindowMaximizeCallback(windowHandle, (window, maximized) -> {
			if (windowListener.onMaximize != null) windowListener.onMaximize.onMaximize(maximized);
		});
		// 设置窗口最小化回调
		GLFW.glfwSetWindowIconifyCallback(windowHandle, (window, minimized) -> {
			if (windowListener.onMinimize != null) windowListener.onMinimize.onMinimize(minimized);
		});
		// 设置窗口焦点回调
		GLFW.glfwSetWindowFocusCallback(windowHandle, (window, focused) -> {
			if (windowListener.onFocus != null) windowListener.onFocus.onFocus(focused);
		});
		// 设置初始投影
		updateProjection(width, height);
		Application.addWindow(this);
	}
	
	/**
	 * 更新投影矩阵
	 * @param w 宽度
	 * @param h 高度
	 */
	private void updateProjection(int w, int h) {
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GL11.glOrtho(0, w, h, 0, -1, 1); // 使用像素坐标系
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
	}
	
	@Override
	public void show() {
		if (windowHandle != -1) {
			GLFW.glfwShowWindow(windowHandle);
			isVisible = true;
		} else {
			throw new IllegalStateException("窗口不能在创建之前显示。");
		}
	}
	
	@Override
	public void hide() {
		if (windowHandle != -1) {
			GLFW.glfwHideWindow(windowHandle);
			isVisible = false;
		} else {
			throw new IllegalStateException("窗口不能在创建之前隐藏。");
		}
	}
	
	@Override
	public void minimize() {
		if (windowHandle != -1) {
			GLFW.glfwIconifyWindow(windowHandle);
			isVisible = false;
		} else {
			throw new IllegalStateException("窗口不能在创建之前最小化。");
		}
	}
	
	@Override
	public void close() {
		if (windowHandle != -1) {
			GLFW.glfwDestroyWindow(windowHandle);
			isVisible = false;
			if (!Application.removeWindow(this)) {
				System.err.println("窗口关闭失败。");
			}
			GLFW.glfwTerminate();
		} else {
			throw new IllegalStateException("窗口不能在创建之前关闭。");
		}
	}
}
